package com.wthink.newMain.cameraSettings

import android.annotation.SuppressLint
import android.os.Bundle
import android.text.TextUtils
import android.view.SurfaceHolder
import android.view.View
import android.widget.AdapterView
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.GridLayoutManager
import com.wthink.hzgj.R
import com.wthink.hzgj.databinding.ActivityCameraSettingBinding
import com.wthink.hzgj.util.DpUtil
import com.wthink.hzgj.util.adapter.BaseViewHolder
import com.wthink.hzgj.util.toast
import com.wthink.newMain.cameraSettings.viewmodel.CameraSettingsViewmodel
import com.wthink.newMain.main.adapter.CameraSettingsAdapter
import kotlinx.android.synthetic.main.activity_camera_setting.*
import kotlinx.android.synthetic.main.item_camera_settings.view.*


@SuppressLint("NewApi")
class CameraSettingsActivity : AppCompatActivity() {

    lateinit var binding: ActivityCameraSettingBinding
    private val viewModel by viewModels<CameraSettingsViewmodel>()

    var isChosen = false
    var spinnerItem = ""

    var originPosition = -1

    var lastPosition = -1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_camera_setting)
        binding.lifecycleOwner = this
        binding.viewModel = viewModel
        init()
        initCamera()
    }

    private fun init() {

        viewModel.start()

        binding.cameraRecyler.layoutManager = GridLayoutManager(this, 4)


        binding.cameraRecyler.adapter = object : CameraSettingsAdapter() {
            override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
                super.onBindViewHolder(holder, position)
                val itemView = holder.itemView
                itemView.icon.isSelected = false
                itemView.setOnClickListener {
                    if (lastPosition == -1) {
                        lastPosition = position
                    } else {
                        notifyItemChanged(lastPosition)
                    }
                    isChosen = true
                    itemView.icon.isSelected = true
                    lastPosition = position
//                    val position1 = lastPosition
//                    lastPosition = position
//                    notifyItemChanged(position)
//                    notifyItemChanged(position1)
                    viewModel.setSpinnerList()
                    originPosition = position
                }
            }
        }

        binding.btnSure.setOnClickListener {
            if (isChosen && !TextUtils.isEmpty(spinnerItem) && spinnerItem != "选择要调整的频道") {
                formatList()
            } else {
                toast(this, "请选中需要切换的通道")
            }
        }

        binding.cameraSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {

            }

            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                spinnerItem = viewModel.spinnerList.value!![position]
                if (viewModel.spinnerList.value!!.size > 1) {
                    btn_sure.text = "确定"
                    tv_sure.background = null
                }
            }
        }
    }

    private fun formatList() {
        val list = viewModel.cameraList.value!! as ArrayList
        val chosenPosition = list.indexOf(spinnerItem)
        val originData = list[originPosition]
        list[originPosition] = spinnerItem
        list[chosenPosition] = originData
        val parseList = DpUtil.parseList(list)
        viewModel.setConfig(parseList)
    }

    private fun initCamera() {
        binding.surfaceView.holder.addCallback(object : SurfaceHolder.Callback {
            private var mCamera: android.hardware.Camera? = null
            private fun getDefaultCamera(): Int {
                val numberOfCameras = android.hardware.Camera.getNumberOfCameras()
                val cameraInfo = android.hardware.Camera.CameraInfo()

                var defaultCameraId = 0

                for (i in 0 until numberOfCameras) {
                    android.hardware.Camera.getCameraInfo(i, cameraInfo)
                    if (cameraInfo.facing == android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK) {
                        defaultCameraId = i
                        break
                    }
                }
                return defaultCameraId
            }

            override fun surfaceChanged(
                holder: SurfaceHolder?,
                format: Int,
                width: Int,
                height: Int
            ) {
            }

            override fun surfaceDestroyed(holder: SurfaceHolder?) {
                mCamera?.stopPreview()
                mCamera?.release()
                mCamera = null
            }

            override fun surfaceCreated(holder: SurfaceHolder?) {
                mCamera = android.hardware.Camera.open(getDefaultCamera())
                mCamera!!.apply {
                    setPreviewDisplay(holder)
                    startPreview()
                }
            }
        })
    }
}